﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace XNAssist
{
    /// <summary>
    /// A simple cursor class for a texture that follows the mouse location, with some extra little bits. Somewhat designed for inheriting from.
    /// </summary>
    public class Cursor
    {
        /// <summary>
        /// The texture to be drawn
        /// </summary>
        public Texture2D CursorTexture { get; protected set; }

        /// <summary>
        /// The offset of the texture from the actual mouse location
        /// </summary>
        public Vector2 Offset { get; protected set; }

        /// <summary>
        /// The scale of the cursor texture
        /// </summary>
        public Vector2 Scale { get; protected set; }

        /// <summary>
        /// Whether the center of the texture is at the mouse location or the upper left is
        /// </summary>
        public bool Centered { get; protected set; }

        /// <summary>
        /// Whether the cursor is visible or not
        /// </summary>
        public bool Visible { get; set; }

        /// <summary>
        /// A simple constructor
        /// </summary>
        /// <param name="cursorTexture"> The texture of the cursor </param>
        public Cursor(Texture2D cursorTexture)
        {
            CursorTexture = cursorTexture;
            Offset = Vector2.Zero;
            Scale = new Vector2(1);
            Visible = true;
        }

        /// <summary>
        /// A simple constructor
        /// </summary>
        /// <param name="cursorTexture"> The texture of the cursor </param>
        /// <param name="centered"> Whether the cursor texture is centered on the cursor or based on the upper left of the texture </param>
        public Cursor(Texture2D cursorTexture, bool centered)
        {
            CursorTexture = cursorTexture;
            Offset = Vector2.Zero;
            Scale = new Vector2(1);
            Centered = centered;
            Visible = true;
        }

        /// <summary>
        /// A simple constructor
        /// </summary>
        /// <param name="cursorTexture"> The texture of the cursor </param>
        /// <param name="scale"> The scale of the texture </param>
        public Cursor(Texture2D cursorTexture, Vector2 scale)
        {
            CursorTexture = cursorTexture;
            Offset = Vector2.Zero;
            Scale = scale;
            Visible = true;
        }

        /// <summary>
        /// A simple constructor
        /// </summary>
        /// <param name="cursorTexture"> The texture of the cursor </param>
        /// <param name="scale"> The scale of the texture </param>
        /// <param name="centered"> Whether the cursor texture is centered on the cursor or based on the upper left of the texture </param>
        public Cursor(Texture2D cursorTexture, Vector2 scale, bool centered)
        {
            CursorTexture = cursorTexture;
            Offset = Vector2.Zero;
            Scale = scale;
            Centered = centered;
            Visible = true;
        }

        /// <summary>
        /// A simple constructor
        /// </summary>
        /// <param name="cursorTexture"> The texture of the cursor </param>
        /// <param name="scale"> The scale of the texture </param>
        /// <param name="offset"> The offset of the texture from the actual cursor position </param>
        public Cursor(Texture2D cursorTexture, Vector2 scale, Vector2 offset)
        {
            CursorTexture = cursorTexture;
            Scale = scale;
            Offset = offset;
            Visible = true;
        }

        /// <summary>
        /// A simple constructor
        /// </summary>
        /// <param name="cursorTexture"> The texture of the cursor </param>
        /// <param name="scale"> The scale of the texture </param>
        /// <param name="offset"> The offset of the texture from the actual cursor position </param>
        /// <param name="centered"> Whether the cursor texture is centered on the cursor or based on the upper left of the texture </param>
        public Cursor(Texture2D cursorTexture, Vector2 scale, Vector2 offset, bool centered)
        {
            CursorTexture = cursorTexture;
            Scale = scale;
            Offset = offset;
            Centered = centered;
            Visible = true;
        }

        /// <summary>
        /// Draws the cursor with the color white
        /// </summary>
        /// <param name="spriteBatch"> The SpriteBatch to draw to </param>
        public virtual void Draw(SpriteBatch spriteBatch)
        {
            Draw(spriteBatch, Color.White);
        }

        /// <summary>
        /// Draws the cursor with the given color
        /// </summary>
        /// <param name="spriteBatch"> The SpriteBatch to draw to </param>
        /// <param name="color"> The color to draw with </param>
        public virtual void Draw(SpriteBatch spriteBatch, Color color)
        {
            if (!Visible) return;

            var mousePos = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);

            mousePos += Offset;

            spriteBatch.Draw(CursorTexture, mousePos, null, color, 0f,
                Centered ? new Vector2(CursorTexture.Width/2, CursorTexture.Height/2) : Vector2.Zero, Scale,
                SpriteEffects.None, 0f);
        }

        /// <summary>
        /// Sets the texture of the cursor
        /// </summary>
        /// <param name="texture"> The new texture </param>
        public void SetTexture(Texture2D texture)
        {
            CursorTexture = texture;
        }
    }
}
